package com.wang.store.controller;

import com.wang.store.entity.Address;
import com.wang.store.service.IAddressService;
import com.wang.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("address")
@RestController
public class AddressController extends BaseController{
    @Autowired
    private IAddressService addressService;

    @RequestMapping("add_new_address")
    public JsonResult<Void> addNewAddress(HttpSession session , Address address){
        Integer uid = getuidFromSession(session);
        String username = getUsernameFromSession(session);
        addressService.addNewAddress(address,uid,username);
        return new JsonResult<Void>(OK);
    }

    @RequestMapping({"/",""})
    public JsonResult<List<Address>> getAddressByUid(HttpSession session){
        Integer getuid = getuidFromSession(session);
        List<Address> data = addressService.getByuid(getuid);
        return new JsonResult<>(OK,data);

    }

    @RequestMapping("{aid}/set_default")
    public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid,HttpSession session){
            addressService.setDefault(aid,getuidFromSession(session),
                    getUsernameFromSession(session));
            return new JsonResult<>(OK);
    }

    @RequestMapping("{aid}/delete")
    public JsonResult<Void> delete(@PathVariable("aid") Integer aid ,HttpSession session){
        addressService.delete(aid,getuidFromSession(session),getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }




}
